// 权限问题后期增加
import {get, post, put, deleteRequest} from '/@/utils/http/axios';
import {UserState} from '/@/store/modules/user/types';
import {ADMIN_USER_TOKEN, AUTHORIZATION} from "/@/store/constants";

// import axios from 'axios';
enum URL {
    // 后台用户登录
    login = '/api/admin/login',
    // 用户列表
    userList = '/api/system/user',
    // 用户详情
    detail = '/api/system/user',
    // 新增用户
    create = '/api/system/user',
    // 修改用户
    update = '/api/system/user',
    // 删除用户
    delete = '/api/system/user/delete',
    // 前台用户登录
    userLogin = '/api/user/userLogin',
    // 获取Token
    oauthToken = '/api/oauth/token',

    updateUserPwd = '/api/user/updatePwd',
    updateUserInfo = '/api/user/updateUserInfo',
}

interface LoginRes {
    token: string;
}

export interface LoginData {
    username: string;
    password: string;
}

const token =  localStorage.getItem(AUTHORIZATION);

const loginApi = async (data: LoginData) => post<any>({
    url: URL.login,
    data,
    headers: {'Content-Type': 'application/json'}
});
const userListApi = async (params: any) => get<any>({
    url: URL.userList, params: params, data: {},
    headers: {
        'Authorization': `Bearer ${token}`
    }
});
const detailApi = async (id: number) => get<any>({
    url: URL.detail +"/"+ `${id}`,
    params: {},
    data: {},
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});
const createApi = async (data: any) => post<any>({
    url: URL.create, params: {}, data: data,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});
const updateApi = async (data: any) => put<any>({
    url: URL.update,
    data: data,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});
const deleteApi = async (params: any) => deleteRequest<any>({
    url: URL.delete,
    params: params,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});
const userLoginApi = async (data: LoginData) => post<any>({
    url: URL.userLogin,
    data,
    headers: {'Content-Type': 'multipart/form-data;charset=utf-8'}
});

const updateUserPwdApi = async (params: any) => post<any>({url: URL.updateUserPwd, params: params});
const updateUserInfoApi = async (data: any) => post<any>({
    url: URL.updateUserInfo,
    data: data,
    headers: {'Content-Type': 'multipart/form-data;charset=utf-8'}
});


// 设置Basic Auth的认证信息
const clientId = 'my-trusted-client'; // 从配置中获取
const clientSecret = 'secret'; // 从配置中获取
// const authHeader = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
const authHeader = "bXktdHJ1c3RlZC1jbGllbnQ6c2VjcmV0";



// 获取token的API
const oauthTokenApi = async (data: any) => post<any>({
    url: URL.oauthToken,
    params: {}, data: data,
    headers: {
        'Authorization': `Basic ${authHeader}`,
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});

export {
    loginApi,
    userListApi,
    detailApi,
    createApi,
    updateApi,
    deleteApi,
    userLoginApi,
    updateUserPwdApi,
    updateUserInfoApi,
    oauthTokenApi
};
